var ai = require("../../utils/ai.js")
var util = require("../../utils/tools.js")
var app = getApp()
Page({
  data: {
    isFirst: true,
    originUrl: '',
    tempFilePath: '',
    imageTarget: '',
    picUrl: ''
  },
  onLoad: function (options) {
    var syInfo = wx.getSystemInfoSync();
    console.log("info===", JSON.stringify(syInfo))
    this.setData({
      width: syInfo.windowWidth,
      height: syInfo.windowHeight
    })
    var url = options.url
    var location = options.location
    if (url) {
      url = decodeURIComponent(url)
      location = decodeURIComponent(location)
      var that = this
      wx.downloadFile({
        url: url,
        success: (res) => {
          console.log("res===", JSON.stringify(res))
          that.setData({
            tempFilePath: res.tempFilePath,
            originLocation: location,
            originUrl: url
          })
        }, fail: (res => {
          console.log("fail===", JSON.stringify(res))
        })
      });
    }

  },
  bindSeletcPhoto: function () {
    var that = this
    wx.getSetting({
      success: (res) => {
        if (JSON.stringify(res.authSetting) != "{}" &&
          !res.authSetting['scope.album']) {
          wx.showModal({
            title: '提示',
            content: '是否打开授权设置',
            success:res=>{
              wx.openSetting({
                success: res => {
                  that.openPhoto()
                },
                fail: res => {
                  console.log("======", JSON.stringify(res))
                  app.showToastMsg("请授权相关权限才能使用此功能")
                }
              })
            }
          })
         
        } else {
          that.openPhoto()
        }
      }
    })
  },
  openPhoto: function () {
    var that = this
    var image = ""
    wx.chooseImage({
      count: 1,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success: function (i) {
        var path = i.tempFilePaths && i.tempFilePaths[0] || "";
        that.setData({
          picUrl: path,
          isFirst: false,
        })
        wx.showLoading({
          title: '正在合成中...',
        });
        //把融合的图片转化
        util.localImageTobase64(that.data.tempFilePath).then(data => {
          image = data
          return util.localImageTobase64(path) //   
        }).then(data => {
          return ai.checkBaiduImg(data)
        }).then(data => {
          return ai.detect(data)
        }).then(data => {
          console.log("data===", JSON.stringify(data))
          var targetImage = data.image
          var location = data.location
          return ai.faceMerge(image, targetImage, JSON.stringify(location), that.data.originLocation)
        }).then(data => {
          console.log("data=====", JSON.stringify(data))
          wx.hideLoading();
          app.globalData.imageTarget = data
          wx.navigateTo({
            url: '../save/save'
          });
        }).catch(err => {
          that.setData({
            isFirst: false,
          })
          wx.hideLoading()
          app.showToastMsg(JSON.stringify(err))
          console.log("err===", JSON.stringify(err))
        })
      }
    })
  },
})